const https = require('https');
const buffer = require('buffer')
var fs = require('fs')
// 测试1
var path = require('path')
// let axios = require('axios')
const axios = require('./utils/http.js')
var xlsx = require('node-xlsx');
const cheerio = require('cheerio');
const userAgents = require('./utils/userAgent.js');
const nodeXlsx = require('node-xlsx')
const htmlUrl = 'https://hotels.ctrip.com/hotels/list?countryId=1&city=-1&checkin=2023/03/16&checkout=2023/03/17&optionId=11&optionType=Province&directSearch=1&optionName=%E6%B0%91%E5%AE%BF&display=%E6%B0%91%E5%AE%BF&crn=1&adult=1&children=0&searchBoxArg=t&travelPurpose=0&ctm_ref=ix_sb_dl&domestic=1&'
const getHtml = (url) => {
    return new Promise((resolve,reject) => {
        axios.get(url, {
            // httpsAgent: agent,
            // headers: {
            //     'User-Agent': (new (require('user-agents'))).data.userAgent
            // }
        }).then(res => {
            resolve(res)
        }).catch(() => {
            reject('error-fetch')
        })
    })
}
// getHtml(htmlUrl).then(res => {
//     console.log(res,'13345')
// })
app.get('/p', (req, res) => {								   		//定义路由
    (async () => {
        try {
            const response = await got('https://news.baidu.com/');   //想抓取的网址
            let $ = cheerio.load(response.body)                      //获取网址的DOM结构
            let result = $('#pane-news li a')                        //想抓取的部位
            let news = []                                            //定义新闻数组
            result.each((index, item) => {                           //循环抓取的内容
                news.push($(item).text())                            //循环添加到数组中 
                fs.writeFileSync('./news.txt', $(item).text()+'\n', {flag: 'a'}) //写入文件中
            }) 
            res.send(news)                                           //显示在页面上
        } catch (error) {
            console.log(error);
        }
    })();
 })
//打开服务器端口
app.listen(3001, () => {
    console.log('http://localhost:3001')
 })